<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\User as UserModel;
class User extends Controller
{
    public function register()
    {
		return $this->fetch();
    }
	public function doreg() {
		//dump($_POST);
		$name = $_POST['username'];
		//dump($name);
		$user = UserModel::where('username',$name)->find();
		//dump($user);
		//$username = $user->username;
		//dump($username);
		if (!empty($user)) {
			echo json_encode(['status' => 1]);
		} else {
			echo json_encode(['status' => 0]);
		}
		
	}
	public function checkcode ()
	{
		$captcha = new \think\captcha\Captcha();
		$code = $_POST['code'];
		if (!$captcha->check($code)) {
			echo json_encode(['status' => 2]);
		} else {
			echo json_encode(['status' => 3]);
		}
	}
	public function checknum () 
	{
		$num = $_POST['num'];
		date_default_timezone_set('PRC');
		$url = 'https://api.miaodiyun.com/20150822/industrySMS/sendSMS';
		$code = mt_rand(1000,9999);
		$timestamp = date('YmdHis');
		//echo $timestamp;
		$accountSid = '494e52e8877246d6a0f637b674e9f3b0';
		$authToken = 'dc809f92f627442cafd1ce46cf4b0e4f';
		$data = [
			'accountSid'=>'494e52e8877246d6a0f637b674e9f3b0',
			'smsContent'=>"【啦啦科技】尊敬的用户，您正在进行登陆验证，您的验证码为：{$code},有效时间为3分钟。为了您的信息安全，请妥善保管好您的验证码。 ",
			'to'        =>$num,
			'timestamp' =>$timestamp,
			'sig'       =>md5($accountSid.$authToken.$timestamp)
		];
		$jsonData = '';
		foreach ($data as $key=>$val) {
			$jsonData .=$key .'='.$val.'&';
		}
		$jsonData = rtrim($jsonData,'&');
		//echo $jsonData;
		$headers = [
			'Content-type:application/x-www-form-urlencoded;charset=UTF-8',
			'Accpet:application/json'
		];

		$con = curl_init();
		curl_setopt($con, CURLOPT_URL, $url);
		curl_setopt($con, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($con, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($con, CURLOPT_HEADER, 1);
		curl_setopt($con, CURLOPT_POST, 1);
		curl_setopt($con, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($con, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($con, CURLOPT_POSTFIELDS, $jsonData);
		$result = curl_exec($con);
		curl_close($con);
		echo json_encode(['code' => $code]);
		
	}
	public function reg () 
	{
		dump($_POST);
		//dump($_SERVER);
		$user = new UserModel();
		$username = $_POST['username'];
		$password = $_POST['password'];
		$email = $_POST['email'];
		$phone = $_POST['number'];
		$iP=getenv('REMOTE_ADDR'); 
        $iP_ = getenv('HTTP_X_FORWARDED_FOR'); 
        if (($iP_ != "") && ($iP_ != "unknown")){
            $iP=$iP_;
        }
        if($iP == '::1'){

            $iP = '127.0.0.1';
        }else{
            $iP=getenv('REMOTE_ADDR'); 
        }
       // var_dump(ip2long($IP));
        $ip =  ip2long($iP);
		if ($username == '') {
			
		}
		$user->data([
			'username'=>$username,
			'password'=>$password,
			'email'=>$email,
			'phone'=>$phone,
			'ip'=>$ip
		]);
		//$user->save();
	}
	public function checkphone () 
	{
		$num = $_POST['phone'];
		//dump($name);
		$user = UserModel::where('phone',$num)->find();
		if (!empty($user)) {
			echo json_encode(['status' => 11]);
		} else {
			echo json_encode(['status' => 22]);
		}
	}
	public function login()
	{
		return $this->fetch();
	}
}
